package com.partsoft.dits.integrate;

import java.sql.Types;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.Properties;

import org.springframework.beans.factory.BeanNameAware;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;

import com.partsoft.dits.AbstractDataEdgeDescriptor;
import com.partsoft.dits.DataEdgeDescriptor;
import com.partsoft.dits.TableDataEdgeDescriptor;
import com.partsoft.dits.message.DataProperty;

public class BaseTableDataEdgeDescriptor extends AbstractDataEdgeDescriptor implements DataEdgeDescriptor,
		TableDataEdgeDescriptor, Cloneable, BeanNameAware {

	private String id;

	private String transformScript;

	private String datasourceName;

	private String schemaName;

	private String tableName;

	private List<DataProperty> keyColumns = null;

	private String transformScriptPath;

	public String getTransformScript() {
		return transformScript;
	}

	public void setTransformScript(String transformScript) {
		this.transformScript = transformScript;
	}

	public String getDataSourceName() {
		return datasourceName;
	}

	public void setDataSourceName(String datasourceName) {
		this.datasourceName = datasourceName;
	}

	public String getSchemaName() {
		return schemaName;
	}

	public void setSchemaName(String schemaName) {
		this.schemaName = schemaName;
	}

	public String getTableName() {
		return tableName;
	}

	public void setTableName(String sourceTableName) {
		this.tableName = sourceTableName;
	}

	/**
	 * 非线程安全的。
	 * 
	 * @param keyColumn
	 */
	public void addKeyColumn(DataProperty keyColumn) {
		Assert.notNull(keyColumn);
		if (keyColumns == null) {
			keyColumns = new LinkedList<DataProperty>();
			keyColumns.add(keyColumn);
		} else if (!keyColumns.contains(keyColumn)) {
			keyColumns.add(keyColumn);
		}
	}

	/**
	 * 设置主键字段。
	 * 
	 * @param keyFields
	 *            格式如：COLUMN_NAME=SQL_TYPE_INT， 如果有多个主键请以";"进行分隔。
	 */
	public void setKeyFields(String keyFields) {
		Assert.hasText(keyFields, "必须设置主键字段");
		String[] keys = keyFields.split(";");
		Properties keyProps = StringUtils.splitArrayElementsIntoProperties(keys, "=");
		for (Object oc : keyProps.keySet()) {
			String clnName = oc.toString();
			int keyType = Types.VARCHAR;
			if (StringUtils.hasText(keyProps.getProperty(clnName)))
				try {
					keyType = Integer.parseInt(keyProps.getProperty(clnName));
				} catch (NumberFormatException e) {
				}

			addKeyColumn(new DataProperty(clnName, keyType));
		}
	}

	public String getKeyFields() {
		StringBuffer buffer = new StringBuffer();
		boolean append = false;
		for (DataProperty cln : getKeyColumns()) {
			if (append)
				buffer.append(";");
			append = true;
			buffer.append(cln.getPropertyName()).append("=").append(cln.getValueType());
		}
		return buffer.toString();
	}

	public String getKeyFieldNames() {
		StringBuffer buffer = new StringBuffer();
		boolean append = false;
		for (DataProperty cln : getKeyColumns()) {
			if (append)
				buffer.append(";");
			append = true;
			buffer.append(cln.getPropertyName());
		}
		return buffer.toString();
	}

	public DataProperty getKeyColumn() {
		if (keyColumns == null || keyColumns.size() == 0)
			return null;
		return keyColumns.get(0);
	}

	public void setKeyColumn(DataProperty cln) {
		Assert.notNull(cln);
		clearKeyColumns();
		addKeyColumn(cln);
	}

	protected void clearKeyColumns() {
		keyColumns = null;
	}

	public boolean isSingleKey() {
		if (keyColumns == null || keyColumns.size() == 0)
			return false;
		return keyColumns.size() == 1;
	}

	public boolean isCompositeKey() {
		if (keyColumns == null || keyColumns.size() == 0)
			return false;
		return keyColumns.size() > 1;
	}

	public DataProperty[] getKeyColumns() {
		if (keyColumns == null || keyColumns.size() == 0)
			return new DataProperty[0];
		return keyColumns.toArray(new DataProperty[0]);
	}

	public void setKeyColumns(DataProperty[] keyColumns) {
		this.keyColumns = new ArrayList<DataProperty>(Arrays.asList(keyColumns));
	}

	@Override
	public BaseTableDataEdgeDescriptor clone() {
		BaseTableDataEdgeDescriptor clone = (BaseTableDataEdgeDescriptor) super.clone();
		clone.keyColumns = null;
		if (keyColumns != null && keyColumns.size() > 0) {
			clone.keyColumns = new ArrayList<DataProperty>(keyColumns);
		}
		return clone;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public void setBeanName(String s) {
		setId(s);
	}

	public String getTransformScriptPath() {
		return transformScriptPath;
	}

	public void setTransformScriptPath(String transformScriptPath) {
		this.transformScriptPath = transformScriptPath;
	}

	@Override
	public String toString() {
		return "数据表集成描述符 [id=" + id + ", datasourceName=" + datasourceName + ", schemaName=" + schemaName
				+ ", tableName=" + tableName + ", keyColumns=" + keyColumns + ", transformScriptPath="
				+ transformScriptPath + "]";
	}

}